package com.it.day7;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * ClassName: AtomicIntegerDemo
 * Description: 基本类型原子类
 *
 * @Author: csx
 * @Create: 2025/5/13 - 9:01
 * @version: v1.0
 */
public class AtomicIntegerDemo {
    public static void main(String[] args) throws InterruptedException {
        MyNumber myNumber = new MyNumber();
        CountDownLatch countDownLatch = new CountDownLatch(50);

        for (int i = 0; i < 50; i++) {
            new Thread(() -> {
                try {
                    for (int j = 0; j < 1000; j++) {
                        myNumber.AddPlusPlus();
                    }
                } finally {
                    countDownLatch.countDown();
                }
            }, String.valueOf(i)).start();
        }

        countDownLatch.await(); //等待所有countDown()执行完
        System.out.println(Thread.currentThread().getName() + "\t------result：" + myNumber.atomicInteger.get());
    }
}

class MyNumber{
    AtomicInteger atomicInteger = new AtomicInteger();

    public void AddPlusPlus(){
        atomicInteger.incrementAndGet();
    }
}













